package top.rhynie.web3j.entity;

import java.math.BigDecimal;
import java.util.Map;

public class Block {

  private String blockID;

  private BlockHeader blockHeader;

  public String getBlockID() {
    return blockID;
  }

  public void setBlockID(String blockID) {
    this.blockID = blockID;
  }

  public BlockHeader getBlockHeader() {
    return blockHeader;
  }

  public void setBlockHeader(BlockHeader blockHeader) {
    this.blockHeader = blockHeader;
  }

  @Override
  public String toString() {
    return "Block{" +
        "blockID='" + blockID + '\'' +
        ", blockHeader=" + blockHeader +
        '}';
  }

  public static class BlockHeader {

    private Map<String, String> raw_data;

    private Long number;

    private String txTrieRoot;

    private String witness_address;

    private String parentHash;

    private Integer version;

    private Long timestamp;

    private String witness_signature;

    public Long getNumber() {
      return number;
    }

    public void setNumber(Long number) {
      this.number = number;
    }

    public String getTxTrieRoot() {
      return txTrieRoot;
    }

    public void setTxTrieRoot(String txTrieRoot) {
      this.txTrieRoot = txTrieRoot;
    }

    public String getWitness_address() {
      return witness_address;
    }

    public void setWitness_address(String witness_address) {
      this.witness_address = witness_address;
    }

    public String getParentHash() {
      return parentHash;
    }

    public void setParentHash(String parentHash) {
      this.parentHash = parentHash;
    }

    public Integer getVersion() {
      return version;
    }

    public void setVersion(Integer version) {
      this.version = version;
    }

    public Long getTimestamp() {
      return timestamp;
    }

    public void setTimestamp(Long timestamp) {
      this.timestamp = timestamp;
    }

    public String getWitness_signature() {
      return witness_signature;
    }

    public void setWitness_signature(String witness_signature) {
      this.witness_signature = witness_signature;
    }

    public void convertRawData() {
      this.number = Double.valueOf(raw_data.get("number")).longValue();
      this.txTrieRoot = raw_data.get("txTrieRoot");
      this.witness_address = raw_data.get("witness_address");
      this.parentHash = raw_data.get("parentHash");
      this.version =  Double.valueOf(raw_data.get("version")).intValue();
      this.timestamp = Long.valueOf(new BigDecimal(raw_data.get("timestamp")).toPlainString());
    }

    @Override
    public String toString() {
      return "BlockHeader{" +
          "number=" + number +
          ", txTrieRoot='" + txTrieRoot + '\'' +
          ", witness_address='" + witness_address + '\'' +
          ", parentHash='" + parentHash + '\'' +
          ", version=" + version +
          ", timestamp=" + timestamp +
          ", witness_signature='" + witness_signature + '\'' +
          '}';
    }
  }

}
